package modules.manager
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;

	import modules.GameDispatcher;
	import modules.GameEvent;
	import modules.load.Load;

	public class InformationManager
	{
		public function InformationManager()
		{
			addListeners();
		}

		private static var instance:InformationManager;

		public static function init():void
		{
			logger("提示框模块初始化");

			if (instance == null)
			{
				instance = new InformationManager();
			}
		}

		private var _informationCanvas:MovieClip;

		private function get informationCanvas():MovieClip
		{
			if (_informationCanvas == null)
			{
				_informationCanvas = Load.getInstance("fla.view.lobby.InformationUI");

				_informationCanvas.yesBtn.addEventListener(MouseEvent.CLICK, onClick);
				_informationCanvas.noBtn.addEventListener(MouseEvent.CLICK, onClick);
			}
			return _informationCanvas;
		}

		public var onYesFunc:Function = null;

		public var onNoFunc:Function = null;

		private function get dispatcher():GameDispatcher
		{
			return GameDispatcher.instance;
		}

		/**
		 * 添加侦听器
		 */
		private function addListeners():void
		{
			dispatcher.addEventListener(GameEvent.SHOW_INFORMATION, onShowInformation);

			dispatcher.addEventListener(GameEvent.CLOSE_INFORMATION, function(event:Event):void
			{
				closeViewFunc();
			});
		}

		/**
		 * 显示提示信息
		 **/
		private function onShowInformation(event:GameEvent):void
		{
			var data:Object = event.data;

			informationCanvas.infoTextUI.text = data.infoText;

			onNoFunc = data.clickNoFunction;
			onYesFunc = data.clickYesFunction;

			if (informationCanvas.parent == null)
			{
				UIAllRefer.popLayer.addChild(informationCanvas);
				informationCanvas.x = (UIAllRefer.stage.stageWidth - informationCanvas.width) / 2;
				informationCanvas.y = (UIAllRefer.stage.stageHeight - informationCanvas.height) / 2;

				//根据弹出提示框的需求, 屏蔽按钮 否
				if (data.showNoBtn == false)
				{
					informationCanvas.noBtn.visible = false;
				}
				else
				{
					informationCanvas.noBtn.visible = true;
				}
			}
		}

		/**
		 * 关闭界面
		 **/
		private function closeViewFunc():void
		{
			if (informationCanvas.parent != null)
			{
				informationCanvas.parent.removeChild(informationCanvas);
			}
		}

		private function onClick(event:MouseEvent):void
		{
			switch (event.currentTarget)
			{
				case informationCanvas.yesBtn:
					onYesFunc && onYesFunc();
					closeViewFunc();
					break;
				case informationCanvas.noBtn:
					onNoFunc && onNoFunc();
					closeViewFunc();
					break;
			}
		}
	}
}
